<template>
  <astral></astral>
  <el-row :gutter="0" class="login-body">
    <el-col :md="12" :lg="16" :offset="0" class="login-left">
      <div class="login-left">
        <div>
          <h1 class="h1-style">欢迎光临MMS平台</h1>
          <span class="span-style"
            >这个平台主要是让广大网友学习使用Vue3开发系统</span>
        </div>
      </div>
    </el-col>
    <el-col :md="12" :lg="8" :offset="0" class="login-right">
      <div class="login-info">
        <h1 style="color:#1f1f1f">欢迎回来</h1>
        <div class="up-style">
          <span class="up-span-style"></span>
          <span class="up-span-title">账号密码登录</span>
          <span class="up-span-style"></span>
        </div>
        <el-form
          ref="formRef"
          style="width: 250px"
          :model="userForm"
          :rules="rules"
          size="default"
          status-icon
        >
          <el-form-item prop="username">
            <el-input v-model="userForm.username" placeholder="请输入用户名" prefix-icon="User"/>
          </el-form-item>
          <el-form-item prop="password">
            <el-input v-model="userForm.password" placeholder="请输入密码" show-password prefix-icon="Lock"/>
          </el-form-item>
          <el-form-item>
            <el-button round color="#626aef" class="login-btn" @click="submit" :loading="loading"> 登录 </el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-col>
  </el-row>
</template>
<script setup>
import { ref, reactive, onBeforeUnmount } from 'vue'
import { useStore } from 'vuex'
import { toast } from '~/composables/util'
import { useRouter } from 'vue-router'
import Astral from '~/components/Astral.vue'

const store = useStore()
const router = useRouter()
const formRef = ref(null)
const loading = ref(false)

// 要提交的表单
const userForm = reactive({
  username: "",
  password: "",
})

// 提交时的表单校验
const rules = {
  username: [
    {
      required: true,
      message: "用户名不能为空",
      trigger: "blur",
    },
  ],
  password: [
    {
      required: true,
      message: "密码不能为空",
      trigger: "blur",
    },
  ],
}

// 登录接口
const submit = (e) => {
  if(e.key == undefined || e.key == "Enter"){  
    formRef.value.validate((valid) => {
        if (!valid) return false
        loading.value = true
        store.dispatch("login", userForm).then(() => {
            toast("登录成功")
            router.push('/')
        }).finally(() => {
            loading.value = false
        })
    })
  }
}

// 监听键盘按键，用于回车快捷登录
document.addEventListener("keyup",submit)
onBeforeUnmount(()=>document.removeEventListener("keyup",submit))
</script>
<style lang="scss" scoped>
@mixin center() {
  display: flex;
  justify-content: center;
  align-items: center;
}
.login-body {
  width: 100vw;
  height: 100vh;
  .login-left {
    @include center;
    .h1-style {
      color: #fdfdfd;
      font-size: 44px;
      font-weight: 800;
      text-shadow: 4px 8px 0 #4a4a4a;
    }
    .span-style {
      color: #fdfdfd;
      font-size: 18px;
      font-weight: 400;
    }
  }
  .login-right {
    @include center;
    .login-info {
      @include center;
      flex-direction: column;
      background: #cbd5e1;
      padding: 100px 70px;
      border-radius: 10px;
      opacity: 0.9;
      box-shadow: 0 0 20px 0 #e2e8f0;
      .up-style {
        @include center;
        margin-bottom: 24px;
        .up-span-title {
            margin: 0 4px;
            font-weight: 400;
            color: #9ca3af;
        }
        .up-span-style {
            width:62px;
            height: 1px;
            background:#e5e7eb;
        }
      }
      .login-btn {
          width: 250px;
          color: white;
      }
    }
  }
}
</style>